#include "NewtonInterpolation.hpp"

using namespace std;

// 定义目标函数
double targetFunction(double x) {
    return 1.0 / (1.0 + x * x);
}

// 生成插值数据并保存到文件
void generateInterpolationData(const string& filename, int n) {
    vector<double> x, y;
    double start = -5, end = 5;
    double step = (end - start) / n;

    for (int i = 0; i <= n; ++i) {
        x.push_back(start + i * step);
        y.push_back(targetFunction(x.back()));
    }

    NewtonInterpolation interpolator(x, y);
    ofstream outFile(filename);
    if (!outFile) {
        cerr << "Error opening file for writing." << endl;
        return;
    }

    for (double x_val = -5; x_val <= 5; x_val += 0.01) {
        double interpolated = interpolator(x_val);
        double actual = targetFunction(x_val);
        outFile << fixed << setprecision(6) << x_val << " " << interpolated << " " << actual << endl;
    }

    outFile.close();
}

int main() {
    for (int n : {2, 4, 6, 8}) {
        string filename = "./data/dataB_n" + to_string(n) + ".txt";
        generateInterpolationData(filename, n);
    }
    return 0;
}